package com.jg.visitor.common.service;

import com.jg.visitor.GlobalConst;
import com.jg.visitor.config.autoconfiguration.caffeinecache.service.CacheService;
import com.jg.visitor.domain.GlobalConfig;
import com.jg.visitor.service.GlobalConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 小狐狸
 */
@Slf4j
@Component
public class GlobalConfigInitService {

    @Resource
    private GlobalConfigService globalConfigService;
    @Resource
    private CacheService cacheService;


    public void initConfigToCache(){

        //查询所有的配置
        List<GlobalConfig> configList = globalConfigService.list();

        configList.forEach(config -> log.info(config.getRemark() + "===================>" + config.getConfigValue()));

        //转成map
        Map<String, GlobalConfig> configMap = configList
                .stream()
                .collect(Collectors.toMap(GlobalConfig::getConfigKey, a -> a));

        //加入缓存
        cacheService.put(GlobalConst.GLOBAL_CONFIG_CODE,GlobalConst.GLOBAL_CONFIG_KEY,configMap);
    }
}
